package com.byyy.ccts.modules.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.byyy.ccts.modules.sys.entity.*;
import com.byyy.ccts.modules.sys.service.IDictService;
import com.byyy.ccts.modules.sys.service.IMenuService;
import com.byyy.ccts.modules.sys.service.IRoleDataPermissionService;
import com.byyy.ccts.modules.sys.service.IRoleMenuService;
import com.byyy.ccts.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("${admin.url.prefix}/sys/role")
@RequiresPathPermission("sys:role")
public class RoleController extends BaseCRUDController<Role, String> {
	
	@Autowired
	private IMenuService menuService;
	@Autowired
	private IRoleMenuService roleMenuService;
	@Autowired
	private IDictService dictService;
	@Autowired
	private IRoleDataPermissionService roleDataPermissionService;

	/**
	 * 角色功能权限界面
	 * @param role
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "authMenu", method = RequestMethod.GET)
	public String authMenu(Role role, Model model, HttpServletRequest request, HttpServletResponse response) {
		EntityWrapper<Menu> entityWrapper = new EntityWrapper<Menu>(Menu.class);
		entityWrapper.orderBy("sort", false);
		List<Menu> menus = menuService.selectTreeList(entityWrapper);
		List<Menu> selectMenus = menuService.findMenuByRoleId(role.getId());
		model.addAttribute("menus", JSONArray.toJSON(menus));
		model.addAttribute("selectMenus", JSONArray.toJSON(selectMenus));
		model.addAttribute("data", role);
		return display("authMenu");
	}

	/**
	 * 角色功能权限设置
	 * @param role
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/doAuthMenu", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson doAuthMenu(Role role, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.success("功能权限设置成功");
		String roleId = role.getId();
		// 删除菜单关联
		roleMenuService.delete(new EntityWrapper<RoleMenu>(RoleMenu.class).eq("roleId", roleId));
		String selectMenu = request.getParameter("selectMenus");
		String[] selectMenus = selectMenu.split(",");
		List<RoleMenu> roleMenuList = new ArrayList<RoleMenu>();
		for (String menuId : selectMenus) {
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setRoleId(roleId);
			roleMenu.setMenuId(menuId);
			roleMenuList.add(roleMenu);
		}
		roleMenuService.insertOrUpdateBatch(roleMenuList);
		UserUtils.clearCache();
		return ajaxJson;
	}

	/**
	 * 角色数据权限页面
	 * @param role
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "dataPermission", method = RequestMethod.GET)
	public String dataPermission(Role role, Model model, HttpServletRequest request, HttpServletResponse response) {
		List<Dict> dataPermissions = dictService.selectDataPermissionTreeList();
		List<Dict> selectDataPermissions = dictService.findDataPermissionByRoleId(role.getId());
		model.addAttribute("dataPermissions", JSONArray.toJSON(dataPermissions));
		model.addAttribute("selectDataPermissions", JSONArray.toJSON(selectDataPermissions));
		model.addAttribute("data", role);
		return display("dataPermission");
	}

	/**
	 * 角色数据权限设置
	 * @param role
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/doDataPermission", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson doDataPermission(Role role, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson ajaxJson = new AjaxJson();
		ajaxJson.success("数据权限设置成功");
		String roleId = role.getId();
		// 删除菜单关联
		roleDataPermissionService.delete(new EntityWrapper<RoleDataPermission>(RoleDataPermission.class).eq("roleId", roleId));
		String selectDataPermission = request.getParameter("selectDataPermissions");
		String[] selectDataPermissions = selectDataPermission.split(",");
		List<RoleDataPermission> roleDataPermissionList = new ArrayList<RoleDataPermission>();
		RoleDataPermission roleDataPermission = null;
		for (String dataPermissionId : selectDataPermissions) {
			roleDataPermission = new RoleDataPermission();
			roleDataPermission.setRoleId(roleId);
			roleDataPermission.setDatapermissionId(dataPermissionId);
			roleDataPermissionList.add(roleDataPermission);
		}
		roleDataPermissionService.insertOrUpdateBatch(roleDataPermissionList);
		UserUtils.clearCache();
		return ajaxJson;
	}

}
